//
//  UserAccountViewModel.swift
//  sina
//
//  Created by DXapple on 15/11/13.
//  Copyright © 2015年 sqlite. All rights reserved.
//

import UIKit
import AFNetworking
class UserAccountViewModel: NSObject {
    var account: UserAccount?
    
    
    //判断用户是否登陆
    var userLogin: Bool {
    
        return account?.access_token != nil
    
    }
    var token: String? {
    
        return account?.access_token
    }
    var userName: String? {
        
        return account?.name
    }
    
    var headURL: NSURL? {
        return NSURL(string: (account?.avatar_large ?? "") )
        
    }
    override init() {
        account = UserAccount.loadAccount()
    }
    
    
    func loadAccessToken(code: String, finished: (error: NSError?) ->()  ) {
        
        let urlString = "oauth2/access_token"
        
        let paramenters = ["client_id": client_id,"client_secret":client_secret,"grant_type" : "authorization_code","code":code,"redirect_uri":redirect_uri]
        
        NetworkTools.sharedTools.requestJsonDict(.POST, urlString: urlString, parmaters: paramenters) { (result, error) -> () in
                  print(urlString,"进来了-------------------------------")
            if error != nil {
            print(error)
            finished(error: error)
                return
            }
            //执行成功
            let account = UserAccount(dict: result!)
            self.loadUserInfo(account, finished: finished)
            
        }
        
//        let AFN = AFHTTPSessionManager()
//        
//        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
//        
//        AFN.POST(urlString, parameters: paramenters, success: { (_ , resl) -> Void in
//            
//            if let dict = resl as? [String : AnyObject] {
//                
//                let account = UserAccount(dict: dict)
//                
//                self.loadUserInfo(account, finished:  finished)
//                
//            }
//            }) { (_ , error ) -> Void in
//                print(error)
//        }
        
    }

    
    
    private func loadUserInfo(account: UserAccount, finished: (error: NSError?) ->()  ) {
        let urlString = "2/users/show.json"
        let parmaters = ["access_token":account.access_token!,"uid": account.uid!]
       
        NetworkTools.sharedTools.requestJsonDict(.GET, urlString: urlString, parmaters: parmaters) { (result, error) -> () in
            
            if error != nil {
            finished(error: error)
                return
            }
            account.avatar_large = result!["avatar_large"] as? String
            account.name = result!["name"] as? String
            account.saveAcoount()
            finished(error: nil)
            
            
        }
        
        
//        let AFN = AFHTTPSessionManager()
//        AFN .GET(urlString, parameters: parmaters, success: { (_, result) -> Void in
//            //            print(result)
//            if let dict = result as? [String : AnyObject] {
//                
//                account.avatar_large = dict ["avatar_large"] as? String
//                account.name = dict ["name"] as? String
//                
//                account.saveAcoount()
//                finished(error: nil)
//                
//            }
//            
//            }) { (_ , error ) -> Void in
//                finished(error: error)
//                print(error )
//        }
    }


    
    
    
    
}
